package sys.person;

import sys.database.Database;
import sys.util.EventUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Player extends AbstractPerson{

    String eventID;
    String eventName;
    double score;

    public Player(String id, String name, String eventID, String eventName) {
        super(id, name);
        this.eventID = eventID;
        this.eventName = eventName;
    }

    public Player(String id) {
        super(id, "");
    }

    public String getEventID() {
        return eventID;
    }

    public void setEventID(String eventID) {
        this.eventID = eventID;
    }

    public String getEventName() {
        return eventName;
    }

    public void setEventName(String eventName) {
        this.eventName = eventName;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }
    // OK
    public void checkMyRankOfEvent(String eventID) {
        String sql = "select * from player where eventID = " + eventID + "order by score desc";
        Database database = new Database();
        ResultSet table = database.findTable(sql);
        boolean hasPrint = false;
        int rank = 1;
        try {
            while(table.next()) {
                String playerID = table.getString("playerID");
                String playerName = table.getString("playerName");
                double playerScore = table.getDouble("score");

                if(!hasPrint) {
                    System.out.println("项目的排名情况：\n");
                    System.out.println("排名" + "    " + "学/工号" + "  " + "姓名" + "    " + "得分" + "    ");
                    hasPrint = true;
                }
                System.out.println("  " + rank + "  " + "  " + playerID + "  " + "  " + playerName + "  " + "  " + playerScore);
                ++rank;
            }
            --rank;
            System.out.println("\n你在项目 " + EventUtil.getNameOfEventByID(eventID) + " 的排名为 " + rank + "\n");
        } catch (SQLException e) {
            System.out.println("排名查看失败！");
        }
    }

    /**
     * 查看自己的报名项目
     */
    // OK
    public void checkMyEvent() {
        String sql = "select * from player where playerID = " + getId();
        Database database = new Database();
        Statement stmt = database.createConnection();
        try {
            ResultSet resultSet = stmt.executeQuery(sql);
            System.out.println("你的报名项目：");
            System.out.println("项目编号" + "   " + "项目名称");
            while (resultSet.next()) {
                String eventID = resultSet.getString("eventID");
                String eventName = resultSet.getString("eventName");
                System.out.println(eventID + "     " + eventName);
            }
        } catch (SQLException e) {
            System.out.println("查找失败！");
        }
    }
}
